package com.Lgwd.service.userService.userImpl;

import com.Lgwd.dao.BackDao;
import com.Lgwd.dao.UserDao;
import com.Lgwd.entity.BackInfoEntity;
import com.Lgwd.entity.UserAccountEntiy;
import com.Lgwd.entity.UserEntiy;
import com.Lgwd.entity.UserItemInfoEntity;
import com.Lgwd.repository.UserRepository;
import com.Lgwd.service.backService.BackInfoService;
import com.Lgwd.service.backService.UserItemInfoService;
import com.Lgwd.service.userService.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserRepository userRepository;

    @Autowired
    BackInfoService backInfoService;
    @Autowired
    UserItemInfoService userItemInfoService;

    /**
     * 根据账号密码查询游戏角色信息
     *
     * @param userAccountEntiy
     * @return
     */
    @Override
    public UserEntiy findUserInfo(UserAccountEntiy userAccountEntiy) {

        return userRepository.findByUser_accountAnduser_AndUser_pwd(userAccountEntiy.getUser_account(), userAccountEntiy.getUser_pwd());
    }

    /**
     * 角色经验足够升级接口
     * 升级之前减去所穿戴的装备属性再升级 完成后再把属性加上去
     *
     * @param userEntiy
     * @return
     */
    @Override
    public UserEntiy updateUserLv(UserEntiy userEntiy) {
        UserEntiy user = userRepository.findByUser_account(userEntiy.getUser_account());
        if(user.getUser_ex()>=user.getUser_upex()){
            //根据账号查询穿戴了多少装备
            BackDao backDao = new BackDao();
            backDao.setUser_account(userEntiy.getUser_account());
            List<BackInfoEntity> backInfoEntityList = backInfoService.findAllByUserAccountAndFlag(backDao);

            List<UserItemInfoEntity> list = new ArrayList<>();
            for (int i = 0; i < backInfoEntityList.size(); i++) {
                if (backInfoEntityList.get(i).getEquip() == 1) {

                    UserItemInfoEntity userItemInfoEntity = userItemInfoService.findItemInfoByItemIdString(backInfoEntityList.get(i).getItem_id());
                    list.add(userItemInfoEntity);
                    user = jItemInfo(user, userItemInfoEntity);
                }
            }

            Integer ex=user.getUser_ex()-user.getUser_upex();
            user.setUser_ex(ex);

            updateUserInfo(user);

            //升级完毕后加上装备属性
            user=addItemInfo(user, list);
            userRepository.saveAndFlush(user);

            return user;
        }else {
            return null;
        }

    }

    @Override
    public UserEntiy findUserInfoByAccount(UserDao userDao) {
        UserEntiy user = userRepository.findByUser_account(userDao.getUser_account());
        return user;
    }

    /**
     * 查询当前角色属性  经验值
     * <p>
     * 更新经验值 并返回更新后的数据
     *
     * @param userEntiy
     * @return
     */
    @Override
    public UserEntiy findUserGiveUp(UserEntiy userEntiy) {
        //接收超出的经验值
        int outEx = 0;
        //获取查询到的角色 更新经验条
        UserEntiy user = userRepository.findByUser_account(userEntiy.getUser_account());
        /**
         *   计算该次经验值是否会超出升级的经验值 是的话 调用升级功能  并且更新已有经验值和最新的升级经验值
         */
        //已拥有经验  加   获得的经验   等于 现有经验值
        int ex = user.getUser_ex() + userEntiy.getUser_ex();
        //  现有经验值      如果超出所需升级经验  超出部分算作下级经验  更新下级最大经验值
//        if(ex>=user.getUser_upex()){
//            //获得超出的经验值
//            outEx=ex-user.getUser_upex();
//            //传入超出的经验作为下一级最新的经验值 调用升级功能
//            user.setUser_ex(outEx);
//            updateUserInfo(user);
//        }else {
//            user.setUser_ex(ex);
//            userRepository.saveAndFlush(user);
//        }
        user.setUser_ex(ex);
        userRepository.saveAndFlush(user);

        return user;
    }


    /**
     * 角色升级 属性变更
     *
     * @param userEntiy
     */
    @Override
    public void updateUserInfo(UserEntiy userEntiy) {
        if (userEntiy.getUser_profession().equals("1")) {
            //战士升级属性
            userEntiy.setUser_A(updateAres(userEntiy.getUser_A(), "a"));
            userEntiy.setUser_DF(updateAres(userEntiy.getUser_DF(), "df"));
            userEntiy.setUser_HP(updateAres(userEntiy.getUser_HP(), "hp"));
            userEntiy.setUser_MP(updateAres(userEntiy.getUser_MP(), "mp"));
            userEntiy.setUser_M(updateAres(userEntiy.getUser_M(), "m"));
            userEntiy.setUser_lv(userEntiy.getUser_lv() + 1);
            userEntiy.setUser_upex(updateAres(userEntiy.getUser_upex(), "upex"));
            userEntiy.setUser_property(updateAres(userEntiy.getUser_property(), "pr"));//传入攻击力A
            log.info("升级完：：："+userEntiy);
            userRepository.saveAndFlush(userEntiy);
        } else {
            //法师升级属性
            userEntiy.setUser_A(updateMaster(userEntiy.getUser_A(), "a"));
            userEntiy.setUser_DF(updateMaster(userEntiy.getUser_DF(), "df"));
            userEntiy.setUser_HP(updateMaster(userEntiy.getUser_HP(), "hp"));
            userEntiy.setUser_MP(updateMaster(userEntiy.getUser_MP(), "mp"));
            userEntiy.setUser_M(updateMaster(userEntiy.getUser_M(), "m"));
            userEntiy.setUser_lv(userEntiy.getUser_lv() + 1);
            userEntiy.setUser_upex(updateAres(userEntiy.getUser_upex(), "upex"));
            userEntiy.setUser_property(updateMaster(userEntiy.getUser_property(), "pr"));//传入魔攻M
            log.info("升级完：：："+userEntiy);
            userRepository.saveAndFlush(userEntiy);
        }


    }

    /**
     * 战士属性变更计算器
     * 角色属性成长：
     * 战士成长
     * 1.HP+8%
     * 2.MP+5%
     * 3.攻击成长+10%
     * 4.全属性成长+11%
     * 5.防御成长+10%
     * 6.魔攻成长+5%
     * 7.经验成长上一级经验+上一级经验*25%
     */
    public int updateAres(int data, String property) {
        int i = data;
        double b = 0;
        if (property.equals("hp")) {
            b = i + i * 0.08 + 0.5;
        }
        if (property.equals("mp")) {
            b = i + i * 0.05 + 0.5;
        }
        if (property.equals("a")) {
            b = i + i * 0.1 + 0.5;
        }
        if (property.equals("pr")) {
            b = i + i * 0.11 + 0.5;
        }
        if (property.equals("df")) {
            b = i + i * 0.1 + 0.5;
        }
        if (property.equals("m")) {
            b = i + i * 0.05 + 0.5;
        }
        if (property.equals("upex")) {
            b = i + i * 0.25 + 0.5;
        }

        int c = (int) Math.round(b);
        return c;
    }

    /**
     * 法师成长
     * 1.HP+5%
     * 2.MP+8%
     * 3.攻击成长+5%
     * 4.全属性成长+12%
     * 5.防御成长+8%
     * 6魔攻成长+10%
     * 7.经验成长上一级经验+上一级经验*25%
     */
    public int updateMaster(int data, String property) {
        int i = data;
        double b = 0;
        if (property.equals("hp")) {
            b = i + (i * 0.05) + 0.5;
        }
        if (property.equals("mp")) {
            b = i + (i * 0.08) + 0.5;
        }
        if (property.equals("a")) {
            b = i + (i * 0.05) + 0.5;
        }
        if (property.equals("pr")) {
            b = i + (i * 0.12) + 0.5;
        }
        if (property.equals("df")) {
            b = i + (i * 0.08) + 0.5;
        }
        if (property.equals("m")) {
            b = i + (i * 0.1) + 0.5;
        }
        if (property.equals("upex")) {
            b = i + i * 0.25 + 0.5;
        }

        int c = (int) Math.round(b);
        return c;
    }


    /**
     * 减去装备属性
     */
    public UserEntiy jItemInfo(UserEntiy userEntiy, UserItemInfoEntity userItemInfoEntity) {

        userEntiy.setUser_A(userEntiy.getUser_A() - userItemInfoEntity.getProperty_A());
        userEntiy.setUser_M(userEntiy.getUser_M() - userItemInfoEntity.getProperty_M());
        userEntiy.setUser_MP(userEntiy.getUser_MP() - userItemInfoEntity.getProperty_MP());
        userEntiy.setUser_HP(userEntiy.getUser_HP() - userItemInfoEntity.getProperty_HP());
        userEntiy.setUser_DF(userEntiy.getUser_DF() - userItemInfoEntity.getProperty_DF());
        userEntiy.setUser_property(userEntiy.getUser_property() - userItemInfoEntity.getProperty_P());
        return userEntiy;
    }

    /**
     * 加上装备属性
     */
    public UserEntiy addItemInfo(UserEntiy userEntiy, List<UserItemInfoEntity> list) {

        list.forEach(user -> {
            userEntiy.setUser_A(userEntiy.getUser_A() + user.getProperty_A());
            userEntiy.setUser_M(userEntiy.getUser_M() + user.getProperty_M());
            userEntiy.setUser_MP(userEntiy.getUser_MP() + user.getProperty_MP());
            userEntiy.setUser_HP(userEntiy.getUser_HP() + user.getProperty_HP());
            userEntiy.setUser_DF(userEntiy.getUser_DF() + user.getProperty_DF());
            userEntiy.setUser_property(userEntiy.getUser_property() + user.getProperty_P());
        });

        return userEntiy;
    }
}
